//引入http的发生出错误的模块
var createError = require('http-errors');
//引入express模块
var express = require('express');
//引入处理路径模块
var path = require('path');
//引入处理cookie模块
var cookieParser = require('cookie-parser');
//引日志模块
var logger = require('morgan');
//引入的处理路由的文件
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
//引入处理学生的路由
var studentRouter = require('./routes/student');
//创建一个express的对象
var app = express();

//app.use() 注册模块
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//处理静态资源的模块
app.use(express.static(path.join(__dirname, 'public')));
//注册路由
app.use('/', indexRouter);
app.use('/user', usersRouter);
app.use("/student",studentRouter)


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
